package spoon.service;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import spoon.core.domain.Game;

@Service
@Qualifier("tenbetFootballService")
public class TenbetFootballService extends TenbetAbstractService {
	
	@Resource(name = "tenbet.football.league")
	protected void getLeagueMap(Map<String, String> map) {
		this.leagueMap = map;
	}

	@Resource(name = "tenbet.football.team")
	protected void getTeamMap(Map<String, String> map) {
		this.teamMap = map;
	}

	@Value("#{tenbet['football.itemCode']}")
	protected void getItemCode(String itemCode) {
		this.ITEM_CODE = itemCode;
	}

	@Value("#{tenbet['football.itemName']}")
	protected void getItemName(String itemName) {
		this.ITEM_NAME = itemName;
	}

	@Value("#{tenbet['football.url']}")
	protected void getGateUrl(String url) {
		this.GATE_URL = url;	
	}
	

	@Override
	public List<Game> getLeagueList() {
		log.info("===================================================================");
		log.info("    {} {} 리그 정보 검색 시작", SITE, ITEM_CODE);

		// 축구는 리그코드가 1 이다.
		List<Game> leagueList = tenbetHttpService.getLeagueList("1");

		log.info("    {} {} 리그 정보 검색 종료. 전체 추가 리그수 = {}", SITE, ITEM_CODE, leagueList.size());
		log.info("===================================================================");

		return leagueList;
	}

	@Override
	public void getLeague(Game league) {
		
		// 리그 정보
		league.setSite(SITE);
		league.setUrl(URL);
		league.setItemName(ITEM_NAME);
		league.setItemCode(ITEM_CODE);
		league.setNation(""); // 국가정보를 가져올 수 없다.
		league.setLeagueCode(getLeagueCode(league.getLeagueName()));
		
		String source = tenbetHttpService.getGameList("1", league.getLink()); 
		source = source.replaceAll("\",1]", "#");
		
		Pattern p = Pattern.compile("\\[([0-9]+),\\\"([^\\\"]+)\\\",\\\"([^\\\"]+)\\\",([0-9]+),\\\"([^\\\"]+)\\\"([^#]+)");
		Matcher m = p.matcher(source);

		LINK_CODE = league.getBetType();
		
		setTypeMap(league.getBetType());
		
		while (m.find()) {
			Game game = new Game();
			game.setHomeTeamName(m.group(2));
			game.setHomeTeamCode(game.getHomeTeamName());
			game.setAwayTeamName(m.group(3));
			game.setAwayTeamCode(game.getAwayTeamName());
			game.setGameDate(convertDate(m.group(5)));
			game.setLink(convertLink(m.group(6)));
			getBetting(game);
		
			league.addGameList(game);
		}
		
		log.info("----------------------------------------------");
		log.info(league.getLeagueCode());
		for (Game g : league.getGameList()) {
			log.info("{} vs {}", g.getHomeTeamCode(), g.getAwayTeamCode());
			for (Game b : g.getGameList()) {
				log.info("{}", b.toString());
			}
		}
		log.info("----------------------------------------------");
		
	}
	
	private void getBetting(Game game) {
		String source = tenbetHttpService.getBettingList(game.getLink());
		source = source.replaceAll("(\\[[0-9]+,,\\[\\[)", "#$1");
		source = source.substring(2, source.length() - 1) + ",#";
		
		Pattern p = Pattern.compile("\\[([0-9]+),,(\\[[^#]+),([0-9])+\\],#");
		Matcher m = p.matcher(source);

		while (m.find()) {
			String title = linkMap.get(m.group(1));
			String data = m.group(2);

			log.info(title);
			
			if ("Full Time".equals(title)) {
				betMatch(game, data);
			} else if ("Draw No Bet".equals(title)) {
				betDrawNoBet(game, data);
			} else if ("Corners".equals(title)) {
				betCorners(game, data);
			} else if ("Corners 1st Half".equals(title)) {
				betCorners(game, data);
			} else if ("1st Half".equals(title)) {
				betMatch(game, data);
			} else if ("2nd Half".equals(title)) {
				betMatch(game, data);
			}
		}
	}

}
